var ROOT='http://api.hongyanche.com';
    CONSOLE = {
    urls:{
		user_add			: ROOT+'/client/user_add',
		user_set_name		: ROOT+'/client/user_set_name',
		user_list			: ROOT+'/client/user_list',
		user_tel_bind		: ROOT+'/client/user_tel_bind',

		recharge_order		: ROOT+'/client/recharge_order_create',
		recharge_record		: ROOT+'/client/recharge_order_record',
		recharge_refund		: ROOT+'/client/recharge_refund_record',

		card_bind			: ROOT+'/client/card_bind',
		card_unbind			: ROOT+'/client/card_unbind',
		card_record  		: ROOT+'/client/card_record',

		member_record		: ROOT+'/client/member_record',
		member_filter		: ROOT+'/client/member_filter',
		member_check		: ROOT+'/client/member_check',

		pay_admin			: ROOT+'/pay/admin',
		pay_scan			: ROOT+'/pay/scan',
		pay_refund			: ROOT+'/pay/refund',
		pay_query			: ROOT+'/pay/query',

	},
	init:function(){

		CONSOLE.user.init();
		CONSOLE.recharge.init();
		CONSOLE.member.init();
		CONSOLE.cards.init();

		$(document).on('click', "#client-briefing-folder", function(event) {
			var t = $(this);
			if(t.find('.i-fold').css('display') == 'none'){
				t.siblings().fadeIn();
				t.parent().siblings().fadeIn();
				t.find('.i-fold').fadeIn();
				t.find('.i-unfold').hide();
			}else{
				t.siblings().fadeOut(100);
				t.parent().siblings().fadeOut(100);
				t.find('.i-unfold').fadeIn();
				t.find('.i-fold').hide();
			}
		});
	},
	recharge:{
		init:function(){

			$(document).on('click', '.client-recharge-preset-total_fee', function(event) {
				$("#client-recharge-total_fee").val($(this).text())
				$("#client-recharge-total_fee").focus();
				$("#client-recharge-total_fee").trigger('change')
			});
			$(document).on('bc-modal-success','#user-detail-recharge',function(){
				$("#client-recharge-confirm").addClass('weui-btn_disabled');
			})
			$(document).on('change','#client-recharge-total_fee',function(){
				var e = $(this)
				if(parseInt(e.val())>0){
					$("#client-recharge-confirm").removeClass('weui-btn_disabled');
				}else{
					$("#client-recharge-confirm").addClass('weui-btn_disabled');
				}
			})
			var recharge_total_fee,recharge_user_id,recharge_order_id;

			//cash
			POST.init(document,{
				text:"现金支付",
				confirm:{text:"准备订单"},
				url:CONSOLE.urls.recharge_order,
				stop:function(){
					recharge_order_id = 0;
					var p = $('#client-recharge-total_fee').val().replace(/,/g,'');
					recharge_total_fee = parseInt(parseFloat(p)*100);
					if(!recharge_total_fee){
						return true;
					}
					recharge_user_id = parseInt($('#client-recharge-user_id').html());
					if(!recharge_user_id){
						return true;
					}
				},
				input:function(){
					return {total_fee:recharge_total_fee,user_id:recharge_user_id};
				},
				success:function(e,res){
					// console.log(order_id);
					var order_id = res.data.info.order_id;
					if(!order_id){
						alert('order_id订单号错误');
					}
					PAY.order_id = order_id
					$(document).trigger('bc-pay-admin');
				}
			},'#client-recharge-cash-confirm')

			//scan
			POST.init(document,{
				method:'change',
				text:"准备订单",
				url:CONSOLE.urls.recharge_order,
				stop:function(e){
					var auth_code = parseInt($(e).val());
					if(!auth_code || auth_code<1000000) return true;
					recharge_order_id = 0;
					var p = $('#client-recharge-total_fee').val().replace(/,/g,'');
					recharge_total_fee = parseInt(parseFloat(p)*100);
					if(!recharge_total_fee){
						return true;
					}
					recharge_user_id = parseInt($('#client-recharge-user_id').html());
					if(!recharge_user_id){
						return true;
					}
				},
				input:function(){
					return {total_fee:recharge_total_fee,user_id:recharge_user_id};
				},
				success:function(e,res){
					var order_id = res.data.info.order_id;
					if(!order_id){
						alert('order_id订单号错误');
					}
					var tel = $('#client-recharge-tel').html();
					if(!tel) tel = '(未知用户)';
					PAY.body = "用户充值："+tel;
					PAY.order_id = order_id
					PAY.auth_code = $('#client-recharge-cash-scan').val();
					$('#client-recharge-cash-scan').val('');
					$(document).trigger('bc-pay-scan');
				}
			},'#client-recharge-cash-scan')

			//bc-pay-success
			$(document).on('bc-pay-success' , function(event) {
				var user_id = parseInt($('#client-recharge-user_id').html());
				$.ajax({
					url: CONSOLE.urls.member_check,
					type: 'POST',
					data: {user_id:user_id},
					complete:function(){
						CONSOLE.user.reload();
						LIST.reload('#client-record-recharge-list');
					}
				})				
				INFO.reload('#cashier-briefing');	
			});

			//refund
			var refund_fee,refund_order_id;
			POST.init(document,{
				url:CONSOLE.urls.pay_refund,
				success:function(){
					CONSOLE.user.reload();
					LIST.reload('#client-recharge-refund-list');
					LIST.reload('#client-record-recharge-list');
					INFO.reload('#cashier-briefing');
				},
				text:"退款",
				input:function(){
					return {order_id:refund_order_id,refund_fee:refund_fee}
				},
				confirm:true,
				stop:function(){
					refund_order_id = 0;refund_fee=0;
					refund_fee = parseInt(parseFloat($('#client-recharge-refund_fee').val())*100)
					if(!refund_fee) return true;
					refund_order_id = $('#client-refund-order_id').html();
					if(!refund_order_id) return true;
				}
			},'#client-recharge-refund-confirm')

			//query
			POST.init(document,{
				url:CONSOLE.urls.pay_query,
				text:"刷新",
				input:function(){
					return {order_id:refund_order_id}
				},
				stop:function(){
					refund_order_id = 0;
					refund_order_id = $('#client-refund-order_id').html();
					if(!refund_order_id) return true;
				},
				success:function(){
					$('#client-recharge-refund unpaid').each(function(index, el) {
						VALUE.write($(el),'modal',0);
						$('#client-record-recharge-list-unpaid').prop('checked',false).trigger('change');
					});
					INFO.reload('#cashier-briefing');
				}
			},'#recharge-order-unpaid-refresh')
		}
	},
	member:{
		init:function(){
			POST.init(document,{
				url:CONSOLE.urls.member_check,
				text:"加载会员",
				success:function(){
					CONSOLE.user.reload();
				},
				input:function(){
					var user_id = $("#user-info-user_id").html();
					return {user_id:user_id}
				}
			},'#client-member-check');
		}
	},
	cards:{
		init:function(){
			POST.init(document,{
				stop:function(e){
					var card_id = $(e).parents('.card-tag').find('user_card_id').html();
					console.log($(e),card_id)
					if(!card_id) return true;
				},
				text:'解除绑定',
				input:function(e){
					var card_id = $(e).parents('.card-tag').find('user_card_id').html();
					return {card_id:card_id};
				},
				confirm:true,
				url:CONSOLE.urls.card_unbind,
				success:function(e){
					var c = e.parents('.card-tag');
					c.fadeOut(120,function(){
						c.remove()
					})
					LIST.reload('#admin-client-record-card-list');
					// CONSOLE.user.reload();
				},
			},'#user-info-cards b')
			POST.init(document,{
				stop:function(){
					var user_id = $('#client-card-bind-user_id').html();
					if(!user_id) return true;
					var card_code = $('#client-card-bind-card_code').val();
					if(!card_code || card_code.length < 8) return true;
				},
				text:"绑定卡片",
				input:function(){
					var user_id = $('#client-card-bind-user_id').html();
					var card_code = $('#client-card-bind-card_code').val();
					$('#client-card-bind-card_code').val('')
					return {user_id:user_id,card_code:card_code}
				},
				confirm:true,
				url:CONSOLE.urls.card_bind,
				success:function(e,res){
					LIST.reload('#admin-client-record-card-list');
					VALUE.write($('#user-info-cards'),'detail',res.data.cards);
					// CONSOLE.user.reload();
				},
			},'#client-card-bind')
		}
	},
	user:{
		init:function(){

			$(document).on('bc-list-start', '#user-list', function(event) {
				$('#user-detail').hide();
			});
			$(document).on('bc-list-success bc-list-error', '#user-list', function(event) {
				$('#user-list-content').show();
				$('#user-list-content').next().show();
				$('#user-detail').hide();
				if($(this).find('.bc-list-content').children().length == 1){
					CONSOLE.user.detail($(this).find('.bc-list-content').children().eq(0).find('.bc-list-detail-box'));
				}
			});
			$(document).on('click', '#user-list-content .bc-list-line', function(event) {
				CONSOLE.user.detail($(this).find('.bc-list-detail-box'));
			});
			$(document).on( 'click', '#user-detail-back', function(){
				CONSOLE.user.showList();
			})
			//user-add
			POST.init(document,{
				success:function(){
					CONSOLE.user.reload();
				},
				text:"创建新用户",
				url:CONSOLE.urls.user_add,
				confirm:true,
				msg:{
					'chief incorrect':'请添加自己为client admin',
					'authority incorrect':'权限错误',
				}
			},'#client-add')
			//user-set-name
			POST.init(document,{
				url:CONSOLE.urls.user_set_name,
				style:'input',
				method:'change',
				input:function(){
					var user_id = $('#user-info-user_id').html();
					var newname = $('#user-set-name-input').val();
					return {user_id:user_id,newname:newname}
				},
				success:function(){
					var newname = $('#user-set-name-input').val();
					$('#user-info-user_name').html(newname)
				}
			},'#user-set-name-input')
			//tel-bind
			POST.init(document,{
				url:CONSOLE.urls.user_tel_bind,
				stop:function(){
					var user_id = $("#client-tel-binding-user_id").html();
					if(!user_id) return true;
					var tel = $("#client-tel-binding-tel").val();
					if(!ISTEL(tel)){alert('手机号码格式不正确');return true;}
				},
				input:function(){
					var tel = $("#client-tel-binding-tel").val();
					var user_id = $("#client-tel-binding-user_id").html();
					return {tel:tel,user_id:user_id}
				},
				text:"发送",
				success:function(e,res){
					$("#client-tel-binding-confirm").removeClass('weui-btn_disabled');
					var remain = 5;
					if(res.data && typeof(res.data.remain) != 'undefined'){
						remain = res.data.remain
					}
					TIMER.start("#client-tel-binding-send",remain);
					$('#client-tel-binding-code').select();
					$('#client-tel-binding-code').focus();
				},
				error:function(e,res){
					var remain = 5;
					if(res.data && typeof(res.data.remain) != 'undefined'){
						remain = res.data.remain
					}
					TIMER.start("#client-tel-binding-send",remain);
				}
			},"#client-tel-binding-send")
			POST.init(document,{
				url:CONSOLE.urls.user_tel_bind,
				stop:function(){
					var tel = $("#client-tel-binding-tel").val();
					if(!ISTEL(tel)){
						alert('手机号码格式不正确');
						return true;
					}
					var code = $('#client-tel-binding-code').val();
					if(!code){
						alert('请输入验证码');return true;
					}
				},
				input:function(){
					var tel = $("#client-tel-binding-tel").val();
					var code = $('#client-tel-binding-code').val();
					var user_id = $("#client-tel-binding-user_id").html();
					return {tel:tel,user_id:user_id,code:code}
				},
				success:function(){
					var tel = $("#client-tel-binding-tel").val();
					$('#client-list-keywords').val(tel)
					CONSOLE.user.reload();					
				}
			},"#client-tel-binding-confirm")

		},
		reload:function(){
			MODAL.close('.modal');
			LIST.reload('#user-list')
		},
		showList:function(){
			$('#user-list-content').fadeIn();
			$('#user-list-content').next().fadeIn();
			$('#user-detail').hide();
		},
		detail:function(t){
			DETAIL.put("#user-detail",t);
			$('#user-list-content').hide();
			$('#user-list-content').next().hide();
			$('#user-detail').fadeIn();
		}
	},
}
CONSOLE.init();